package ee.maoinvest.frontend.controller.command.enterpriserelationswithelectricitymetersandcontractscommands;

import java.sql.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.model.data.Enterprise;
import ee.maoinvest.backend.model.data.views.EnterpriseContractWithElectricityMeter;
import ee.maoinvest.backend.services.EnterpriseRelationsWithElectricityMetersAndContractsService;
import ee.maoinvest.backend.services.EnterpriseService;
import ee.maoinvest.backend.services.LatestDatesService;
import ee.maoinvest.backend.services.implementations.EnterpriseRelationsWithElectricityMetersAndContractsServiceImpl;
import ee.maoinvest.backend.services.implementations.EnterpriseServiceImpl;
import ee.maoinvest.backend.services.implementations.LatestDatesServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriserelationswithelectricitymetersandcontractsforms.EnterpriseRelationsWithElectricityMetersAndItsContractsFormConverter;
import ee.maoinvest.frontend.controller.requestprocessors.EnterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor;

public class FillEnterpriseRelationsWithElectricityMetersAndContractsFormConverterWithInitialDataCommand implements Command
{
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int OPERATION_UNSUCCESSFUL = 0;
	private final int ID_PARSE_ERROR_IDENTIFICATOR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		EnterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor enterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor = 
				new EnterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor();
		EnterpriseRelationsWithElectricityMetersAndItsContractsFormConverter enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter = null;
		List<EnterpriseContractWithElectricityMeter> listOfAllEnterpriseContractsWithElectricityMeters = null;
		EnterpriseRelationsWithElectricityMetersAndContractsService enterpriseRelationsWithElectricityMetersAndContractsServiceImpl = null;
		EnterpriseService enterpriseServiceImpl = null;
		Enterprise enterpriseWhichRelationsAreBeingModified = null;
		int enterpriseIDWhichRelationsWithElectricityMetersAndContractsToEdit = 
				enterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor.getEnterpriseIDWhichRelationsWithElectricityMetersAndContractsToEdit(request);
		LatestDatesService latestDatesServiceImpl = null;
		
		if (enterpriseIDWhichRelationsWithElectricityMetersAndContractsToEdit != ID_PARSE_ERROR_IDENTIFICATOR)
		{
			latestDatesServiceImpl = new LatestDatesServiceImpl();
			enterpriseRelationsWithElectricityMetersAndContractsServiceImpl = 
					new EnterpriseRelationsWithElectricityMetersAndContractsServiceImpl();
			enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter = 
					new EnterpriseRelationsWithElectricityMetersAndItsContractsFormConverter();
			enterpriseServiceImpl = new EnterpriseServiceImpl();
			Date latestDateAfterWhatDataManipulationIsPossible = latestDatesServiceImpl.getLatestDateAfterWhatDataManipulationIsPossible();
			enterpriseWhichRelationsAreBeingModified = enterpriseServiceImpl.getEnterpriseById(
					enterpriseIDWhichRelationsWithElectricityMetersAndContractsToEdit);
			listOfAllEnterpriseContractsWithElectricityMeters = 
					enterpriseRelationsWithElectricityMetersAndContractsServiceImpl.getAllEnterpriseContractsWithElectricityMeters(
					enterpriseIDWhichRelationsWithElectricityMetersAndContractsToEdit);
			enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter.fillWithInitialData(latestDateAfterWhatDataManipulationIsPossible,
					listOfAllEnterpriseContractsWithElectricityMeters, enterpriseWhichRelationsAreBeingModified);
			request.setAttribute("enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter", 
					enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter);
			
			HttpSession session = request.getSession();
			session.setAttribute("listOfAllEnterpriseContractsWithElectricityMeters", listOfAllEnterpriseContractsWithElectricityMeters);
			session.setAttribute("enterpriseWhichRelationsAreBeingModified", enterpriseWhichRelationsAreBeingModified);
			session.setAttribute("latestDateAfterWhatDataManipulationIsPossible", latestDateAfterWhatDataManipulationIsPossible);
			operationResult = OPERATION_SUCCESSFUL;
			
		}
		else
		{
			operationResult = OPERATION_UNSUCCESSFUL;
		}
		
		return operationResult;
	}
}
